﻿namespace REVISIONMAX20
{
    using System;
    using System.Data;
    using System.Data.SQLite;

    public abstract class ObjectBase
    {
        private DateTime? _created;
        private int _id;
        private REVISIONMAX20.Status _oldstatus;
        private ObjectBase _ownerstatus;
        private int _parentid;
        private REVISIONMAX20.Status _status;
        private string _tablename;
        private DateTime? _updated;

        public ObjectBase(string ptablename, SQLiteConnection con)
        {
            this._tablename = ptablename;
            this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
            this._status = REVISIONMAX20.Status.StatusNew;
            this._id = Common.NextID(con, this._tablename);
        }

        public ObjectBase(string ptablename, SQLiteDataReader reader)
        {
            this._tablename = ptablename;
            this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
            this._status = REVISIONMAX20.Status.StatusPopulated;
            this._id = reader.GetInt32(0);
            this._created = reader.GetValue(1).DateValueOrNull();
            this._updated = reader.GetValue(2).DateValueOrNull();
        }

        public ObjectBase(string ptablename, ObjectBase owner, SQLiteConnection con)
        {
            this._ownerstatus = owner;
            this._tablename = ptablename;
            this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
            this._status = REVISIONMAX20.Status.StatusNew;
            this._id = Common.NextID(con, this._tablename);
            this._parentid = owner.id;
        }

        public ObjectBase(string ptablename, SQLiteDataReader reader, ObjectBase owner)
        {
            this._ownerstatus = owner;
            this._tablename = ptablename;
            this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
            this._status = REVISIONMAX20.Status.StatusPopulated;
            this._id = reader.GetInt32(0);
            this._parentid = reader.GetInt32(1);
            this._created = reader.GetValue(2).DateValueOrNull();
            this._updated = reader.GetValue(3).DateValueOrNull();
        }

        public void Commit()
        {
            this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
        }

        public void Delete()
        {
            this.Status = REVISIONMAX20.Status.StatusDeleted;
        }

        public void Persist()
        {
            this._oldstatus = this._status;
            this._status = REVISIONMAX20.Status.StatusPopulated;
        }

        public void Rollback()
        {
            if (this._oldstatus != REVISIONMAX20.Status.StatusNotInitialized)
            {
                this._status = this._oldstatus;
                this._oldstatus = REVISIONMAX20.Status.StatusNotInitialized;
            }
        }

        public void UpdateCommand(SQLiteCommand command)
        {
            if (this.id == -1)
            {
                throw new Exception("bug");
            }
            SQLiteParameter parameter = new SQLiteParameter("@id");
            parameter.DbType = DbType.Int32;
            command.Parameters.Add(parameter);
            if (this.OwnerStatus != null)
            {
                SQLiteParameter parameter2 = new SQLiteParameter("@parentid");
                parameter2.DbType = DbType.Int32;
                command.Parameters.Add(parameter2);
            }
            SQLiteParameter parameter3 = new SQLiteParameter("@created");
            parameter3.DbType = DbType.DateTime;
            command.Parameters.Add(parameter3);
            SQLiteParameter parameter4 = new SQLiteParameter("@updated");
            parameter4.DbType = DbType.DateTime;
            command.Parameters.Add(parameter4);
            command.Parameters["@id"].Value = this.id;
            if (this.OwnerStatus != null)
            {
                command.Parameters["@parentid"].Value = this.parentid;
            }
            if (this.Status == REVISIONMAX20.Status.StatusNew)
            {
                this._created = new DateTime?(DateTime.Now);
            }
            command.Parameters["@created"].Value = this._created;
            if (this._status == REVISIONMAX20.Status.StatusModified)
            {
                this._updated = new DateTime?(DateTime.Now);
            }
            command.Parameters["@updated"].Value = this._updated;
            command.CommandText = Common.GetCommandByTableNameAndStatus(this.tablename, this.Status);
        }

        public DateTime? created
        {
            get
            {
                return this._created;
            }
        }

        public int id
        {
            get
            {
                return this._id;
            }
        }

        public bool IsDirty
        {
            set
            {
                if (value)
                {
                    this.Status = REVISIONMAX20.Status.StatusModified;
                }
            }
        }

        public ObjectBase OwnerStatus
        {
            get
            {
                return this._ownerstatus;
            }
        }

        public int parentid
        {
            get
            {
                return this._parentid;
            }
        }

        public REVISIONMAX20.Status Status
        {
            get
            {
                return this._status;
            }
            set
            {
                REVISIONMAX20.Status statusNotInitialized = REVISIONMAX20.Status.StatusNotInitialized;
                statusNotInitialized = this._status;
                if (this._status == REVISIONMAX20.Status.StatusNotInitialized)
                {
                    if (value == REVISIONMAX20.Status.StatusNew)
                    {
                        this._status = REVISIONMAX20.Status.StatusNew;
                    }
                    else if (value == REVISIONMAX20.Status.StatusPopulated)
                    {
                        this._status = REVISIONMAX20.Status.StatusPopulated;
                    }
                }
                else if (this._status == REVISIONMAX20.Status.StatusNew)
                {
                    if (value == REVISIONMAX20.Status.StatusPopulated)
                    {
                        this._status = REVISIONMAX20.Status.StatusPopulated;
                    }
                    else if (value == REVISIONMAX20.Status.StatusDeleted)
                    {
                        this._status = REVISIONMAX20.Status.StatusNotInitialized;
                    }
                    else if (value == REVISIONMAX20.Status.StatusNotInitialized)
                    {
                        throw new Exception("Error");
                    }
                }
                else if (this._status == REVISIONMAX20.Status.StatusModified)
                {
                    if (value == REVISIONMAX20.Status.StatusPopulated)
                    {
                        this._status = REVISIONMAX20.Status.StatusPopulated;
                    }
                    else if (value == REVISIONMAX20.Status.StatusDeleted)
                    {
                        this._status = REVISIONMAX20.Status.StatusDeleted;
                    }
                    else
                    {
                        if (value == REVISIONMAX20.Status.StatusNew)
                        {
                            throw new Exception("Error");
                        }
                        if (value == REVISIONMAX20.Status.StatusNotInitialized)
                        {
                            throw new Exception("Error");
                        }
                    }
                }
                else if (this._status == REVISIONMAX20.Status.StatusPopulated)
                {
                    if (value == REVISIONMAX20.Status.StatusDeleted)
                    {
                        this._status = REVISIONMAX20.Status.StatusDeleted;
                    }
                    else if (value == REVISIONMAX20.Status.StatusModified)
                    {
                        this._status = REVISIONMAX20.Status.StatusModified;
                    }
                    else
                    {
                        if (value == REVISIONMAX20.Status.StatusNew)
                        {
                            throw new Exception("Error");
                        }
                        if (value == REVISIONMAX20.Status.StatusNotInitialized)
                        {
                            throw new Exception("Error");
                        }
                    }
                }
                else if ((this._status == REVISIONMAX20.Status.StatusNotInitialized) && (value != REVISIONMAX20.Status.StatusNotInitialized))
                {
                    throw new Exception("Error");
                }
                if (((statusNotInitialized != this._status) && (this._ownerstatus != null)) && ((value == REVISIONMAX20.Status.StatusDeleted) | (value == REVISIONMAX20.Status.StatusModified)))
                {
                    this._ownerstatus.Status = REVISIONMAX20.Status.StatusModified;
                }
                if (((((this._status == REVISIONMAX20.Status.StatusModified) | (this._status == REVISIONMAX20.Status.StatusNew)) & (value == REVISIONMAX20.Status.StatusModified)) && (this._ownerstatus != null)) && (((this._ownerstatus.Status == REVISIONMAX20.Status.StatusModified) | (this._ownerstatus.Status == REVISIONMAX20.Status.StatusNew)) | (this._ownerstatus.Status == REVISIONMAX20.Status.StatusPopulated)))
                {
                }
            }
        }

        public string tablename
        {
            get
            {
                return this._tablename;
            }
        }

        public DateTime? updated
        {
            get
            {
                return this._updated;
            }
        }
    }
}

